#include<iostream>
#include<vector>
#include<queue>
#include<string>
using namespace std;

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};

class Solution {
public:

    void creatstr(TreeNode* root, string& s)
    {
        if (root == nullptr) return;
        s.push_back('(');
        s += to_string(root->val);
        creatstr(root->left, s);
        if (root->left == nullptr && root->right) {
            s.push_back('(');
            s.push_back(')');
        }
        creatstr(root->right, s);
        s.push_back(')');
    }

    string tree2str(TreeNode* root) {
        string s;
        s += to_string(root->val);
        if (root->left == nullptr && root->right) {
            s.push_back('(');
            s.push_back(')');
        }
        creatstr(root->left, s);
        creatstr(root->right, s);
        return s;
    }
};


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
 * right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        if (root == nullptr)
            return {};
        queue<TreeNode*> qu1;
        queue<TreeNode*> qu2;
        qu1.push(root);
        vector<vector<int>> ans;
        while (!qu1.empty() || !qu2.empty()) {
            vector<int> tmp_arr;
            int flag = 0;
            while (!qu1.empty()) {
                flag = 1;
                TreeNode* tmp_p = qu1.front();
                qu1.pop();
                tmp_arr.push_back(tmp_p->val);
                if (tmp_p->left)
                    qu2.push(tmp_p->left);
                if (tmp_p->right)
                    qu2.push(tmp_p->right);
            }
            if (flag) {
                ans.push_back(tmp_arr);
                tmp_arr.clear();
                flag = 0;
            }
            while (!qu2.empty()) {
                flag = 1;
                TreeNode* tmp_p = qu2.front();
                qu2.pop();
                tmp_arr.push_back(tmp_p->val);
                if (tmp_p->left)
                    qu1.push(tmp_p->left);
                if (tmp_p->right)
                    qu1.push(tmp_p->right);
            }
            if (flag) {
                ans.push_back(tmp_arr);
                tmp_arr.clear();
                flag = 0;
            }
        }
        return ans;
    }
};

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:

    TreeNode* ans = nullptr;

    bool IsInTree(TreeNode* root, TreeNode* p, TreeNode* q)
    {
        if (root == nullptr) return false;
        if (root == p || root == q)return true;
        return IsInTree(root->left, p, q) || IsInTree(root->right, p, q);
    }

    void deal(TreeNode* root, TreeNode* p, TreeNode* q)
    {
        if (root == nullptr) return;
        bool leftc = IsInTree(root->left, p, q);
        bool rightc = IsInTree(root->right, p, q);
        // cout << leftc << rightc <<endl;
        if (leftc && rightc
            || ((root == p || root == q) && (leftc || rightc)))
            ans = root;
        deal(root->left, p, q);
        deal(root->right, p, q);
    }

    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if (root == nullptr) return nullptr;
        if (root == p || root == q) return root;
        deal(root, p, q);
        return ans;
    }
};

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:

    TreeNode* ans = nullptr;

    bool IsInTree(TreeNode* root, TreeNode* p)
    {
        if (root == nullptr) return false;
        return root == p || IsInTree(root->left, p) || IsInTree(root->right, p);
    }

    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if (root == nullptr) return nullptr;
        if (root == p || root == q) return root;

        bool pInLeft = IsInTree(root->left, p);
        bool qInLeft = IsInTree(root->left, q);

        bool pInRight = IsInTree(root->right, p);
        bool qInRight = IsInTree(root->right, q);

        if ((pInLeft && qInRight) || (qInLeft && pInRight))
        {
            return root;
        }
        else if (pInLeft && qInLeft) {
            return lowestCommonAncestor(root->left, p, q);
        }
        else if (pInRight && qInRight) {
            return lowestCommonAncestor(root->right, p, q);
        }
        return nullptr;
    }
};